package xan.tm.digital.common.rules;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
import xan.tm.digital.common.entity.User;
import xan.tm.digital.common.enums.DiscountRuleEnum;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: renwq
 * @date: 2025/9/12 16:13
 * @sesc: 规则引擎
 */
@Component
public class RuleEngine<T> implements InitializingBean {
    private static final List<Rule<?>> USER_RULES = new ArrayList<>();  // 通配符，支持混合

    public static <T> List<String> execute(T input) {
        if (input instanceof User) {
            return USER_RULES.stream()
                    .filter(rule -> ((Rule<T>) rule).matches(input))
                    .map(rule -> ((Rule<T>) rule).apply(input))
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        USER_RULES.addAll(Arrays.asList(DiscountRuleEnum.values()));
    }
}
